package com.cy.jt.controller;

import com.cy.jt.pojo.User;
import com.cy.jt.service.UserService;
import com.cy.jt.vo.PageResult;
import com.cy.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: LiuXingYu
 * @date: 2021/8/2 19:26
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    // http://localhost:8091/user/login
    @PostMapping("/login")
    public SysResult Login(@RequestBody User user){
        String token = userService.Login(user);
        if (StringUtils.hasLength(token)){
            return SysResult.success(token);
        }
        return SysResult.fail();
    }

    @GetMapping("/list")
    public SysResult getUserList(PageResult pageResult){
        pageResult = userService.getUserList(pageResult);
        return SysResult.success(pageResult);
    }

    @PutMapping("/status/{id}/{status}")
    public SysResult updateStatus(User user){
        userService.updateStatus(user);
        return SysResult.success();
    }

    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        userService.addUser(user);
        return SysResult.success();
    }

    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){

        User user = userService.findUserById(id);
        return SysResult.success(user);
    }

    @PutMapping("/updateUser")
    public SysResult updateUser(User user){
        userService.updateUser(user);
        return SysResult.success();
    }

    @DeleteMapping("{id}")
    public SysResult deleteUserById(@PathVariable Integer id){
        userService.deleteUserById(id);
        return SysResult.success();
    }
}
